package com.codechuan.didi_web.controller;

import com.codechuan.didi_web.entity.SysUserAccount;
import com.codechuan.didi_web.entity.SysUserConfig;
import com.codechuan.didi_web.mapper.SysUserAccountMapper;
import com.codechuan.didi_web.mapper.SysUserConfigMapper;
import com.codechuan.didi_web.service.LoginControllerService;
import com.codechuan.didi_web.service.SysUserTokenService;
import com.codechuan.didi_web.service.ThreadManagerService;
import com.codechuan.didi_web.utils.ResultUtils;
import com.codechuan.didi_web.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping(value = "/account")
public class LoginController {

    @Resource
    private LoginControllerService loginControllerService;


    @Resource
    private SysUserAccountMapper sysUserAccountMapper;

    @Resource
    private SysUserTokenService sysUserTokenService;

    @Resource
    private SysUserConfigMapper sysUserConfigMapper;

    @Resource
    private ThreadManagerService threadManagerService;
    /**
     * @param keyNumber 卡密
     * @return 返回成功或失败的消息
     * @Deprecated 卡密登录
     */
    @GetMapping("/login")
    private ResultVO<Object> login(String keyNumber, @RequestHeader("token")String token, String account) {
        try {
            if (Objects.equals(keyNumber, "") || keyNumber == null) {
                return ResultUtils.error(500, "卡密不能为空", "");
            }
            if (Objects.equals(account, "") || account == null) {
                return ResultUtils.error(500, "账号不能为空", "");
            }
            return loginControllerService.login(keyNumber, token,account);
        } catch (Exception e) {
            return ResultUtils.error(500, e.getMessage(), "");
        }

    }

    @GetMapping("/start")
    private void start() {
        System.out.println("项目初始化");
        ArrayList<SysUserAccount> sysUserAccounts = sysUserAccountMapper.selectAllstartAccount();
        for (SysUserAccount sysUserAccount : sysUserAccounts) {
            // 获取自己平台token里面的卡密来取滴滴的token
            // 取出重新赋值
            String account = sysUserAccount.getKeyNumber();
            // 获取滴滴的主号token
            String ticket = sysUserTokenService.selectMainTokenByAccount(account);
            // 获取用户配置
            SysUserConfig sysUserConfig = sysUserConfigMapper.selectByPrimaryKey(account);
            // 获取滴滴的小号token集合
            List<String> minTokenList = sysUserTokenService.selectMiniTokenListByAccount(account);
            String sort = "smart_sort";
            // 设置排序规则
            switch (sysUserConfig.getValue2()) {
                case "b":
                    sort = "smart_sort";
                    break;
                case "c":
                    sort = "price_sort";
                    break;
                case "d":
                    sort = "time_sort";
                    break;
                case "e":
                    sort = "distance_sort";
                    break;
            }
            // 设置刷单类型
            switch (String.valueOf(sysUserConfig.getValue1())) {
                // 大厅
                case "1":
                    sysUserConfig.setQueryType("0");
                    break;
                // 行程
                case "2":
                    sysUserConfig.setQueryType("1");
                    break;
            }
            String msg;
            if (sysUserConfig.getQueryType().equals("0")) {
                // 创建大厅请求参数
                String token = URLEncoder.encode(ticket);
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("access_key_id", 2);
                hashMap.put("route_id", 0);
                hashMap.put("app_version", "6.8.8");
                hashMap.put("lng", sysUserConfig.getLng());
                hashMap.put("address_select", new String[0]);
                hashMap.put("session_id", 0);
                HashMap<String, Object> city_center = new HashMap<>();
                city_center.put("lat", sysUserConfig.getLat());
                city_center.put("lng", sysUserConfig.getLng());
                hashMap.put("city_center", city_center);
                hashMap.put("token", token);
                hashMap.put("sort_type", sort); // 排序规则 smart_sort 智能排序 price_sort 价格最高 time_sort 时间最早 distance_sort 距离最近
                hashMap.put("city_name", sysUserConfig.getCityName());
                hashMap.put("tab_id", 2);
                hashMap.put("filter_group", new String[0]);
                hashMap.put("page_index", 0);
                hashMap.put("to_city_id", 0);
                hashMap.put("lat", sysUserConfig.getLat());
                hashMap.put("city_id", sysUserConfig.getCityId());
                // 保存配置到数据库
                threadManagerService.startOrEndThread(account, ticket, hashMap, sysUserConfig, minTokenList);
            } else {

                threadManagerService.startOrEndThread(account, ticket, null, sysUserConfig, minTokenList);
            }
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

    }
}
